<?php

namespace app\jigou\controller\qingdongams\general;

use addons\qingdongams\model\Contacts;
use addons\qingdongams\model\Customer;
use addons\qingdongams\model\Staff;
use app\common\controller\Backend;
use addons\qingdongams\model\SendTemplater;

/**
 * 短信通知模板
 */
class Sms extends Backend
{
    protected $relationSearch = true;
    /**
     * @var \addons\qingdongams\model\SendTemplater
     */
    protected $model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new SendTemplater();
    }


    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $list = $this->model->where(['type' => 'sms'])->paginate();
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 发送短信
     */
    public function send_sms($ids = null)
    {
        $types=input('type');
        if($types == 'customer'){
            $row = Customer::where([
                'id' => $ids,
                'owner_staff_id' => ['in', Staff::getMyStaffIds()]
            ])->find();
        }elseif($types == 'contacts'){
            $row = Contacts::where([
                'id' => $ids,
                'owner_staff_id' => ['in', Staff::getMyStaffIds()]
            ])->find();
        }else{
            $this->error(__('No Results were found'));
        }

        if (empty($row)) {
            $this->error(__('权限不足'));
        }
        if ($this->request->isPost()) {
            $template_id = input('template_id');
            if (empty($template_id)) {
                $this->error(__('Unknown data format'));
            }
            $templater = SendTemplater::where(['id' => $template_id])->find();
            $params = json_decode($templater['content'], true);
            $result = $this->model::sendSms($ids, $templater['number'], $params,$types);
            if ($result == true) {
                $this->success("发送成功");
            } else {
                $this->error('发送失败');
            }
        }
        $templaters = $this->model->where(['type' => 'sms'])->field('id,name,preview')->select();
        $this->assign('templaters', $templaters);
        $this->assign('ids', $ids);

        return $this->view->fetch('batch_send_sms');
    }

    /**
     * 批量发送短信
     */
    public function batch_send_sms($ids = null)
    {
        $types=input('type');
        $ids = json_decode($ids, true);
        if($types == 'customer'){
            $ids = Customer::where([
                'id' => ['in', $ids],
                'owner_staff_id' => ['in', Staff::getMyStaffIds()]
            ])->column('id');
        }elseif($types == 'contacts'){
            $ids = Contacts::where([
                'id' => ['in', $ids],
                'owner_staff_id' => ['in', Staff::getMyStaffIds()]
            ])->column('id');
        }else{
            $this->error(__('No Results were found'));
        }
        if (empty($ids)) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $template_id = input('template_id');
            if (empty($template_id)) {
                $this->error(__('Unknown data format'));
            }
            $templater = SendTemplater::where(['id' => $template_id])->find();
            $params = json_decode($templater['content'], true);
            $error = 0;
            $success = 0;
            foreach ($ids as $id) {
                $result = $this->model::sendSms($id, $templater['number'], $params,$types);
                if ($result == true) {
                    $success++;
                } else {
                    $error++;
                }
            }
            $this->success("成功发送{$success}条,发送失败{$error}条");
        }

        $templaters = $this->model->where(['type' => 'sms'])->field('id,name,preview')->select();
        $this->assign('templaters', $templaters);
        $this->assign('ids', json_encode($ids));

        return $this->view->fetch();
    }



    /**
     * 获取模板详情
     */
    public function getDetail()
    {
        $id = input('id');
        $row = $this->model->where(['type' => 'sms', 'id' => $id])->find();
        if (empty($row)) {
            $this->error('模板不存在');
        }
        $this->success('请求成功', '', $row);
    }
    /**
     * 删除
     */
    public function del($ids = null) {
        if ($this->request->isAjax()) {

            $map['id'] = array('in', $ids);
            $result    = $this->model->destroy($map);
            if (!$result) {
                $this->error('删除失败');
            }
            $this->success('删除成功');
        }

        return $this->view->fetch();
    }
}
